package com.ztjc.day02_operatorxml;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Xml;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.ztjc.day02_operatorxml.domain.SMS;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Android中xml的操作
 */
public class MainActivity extends Activity {

    private ListView lvList;
    private List<SMS> smsList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lvList = (ListView) findViewById(R.id.lv_list);

        createData();
    }

    /**
     * 创建虚拟数据
     */
    private void createData() {
        smsList = new ArrayList<>();
        for (int i = 0; i < 20; i ++) {
            smsList.add(new SMS("1511697072" + i % 10, "消息" + i, new Date().toString(), i % 2));
        }
    }

    /**
     * 序列化XML
     * @param view
     */
    public void write(View view) {
        if (isSDCanUse()) {
            XmlSerializer xmlSerializer = Xml.newSerializer();
            try {
                xmlSerializer.setOutput(new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "/smslist.xml")), "utf-8");
                xmlSerializer.startDocument("utf-8", true);

                xmlSerializer.startTag(null, "smslist");

                for (SMS sms : smsList) {
                    xmlSerializer.startTag(null, "sms");

                    xmlSerializer.startTag(null, "address");
                    xmlSerializer.text(sms.getAddress());
                    xmlSerializer.endTag(null, "address");

                    xmlSerializer.startTag(null, "body");
                    xmlSerializer.text(sms.getBody());
                    xmlSerializer.endTag(null, "body");

                    xmlSerializer.startTag(null, "date");
                    xmlSerializer.text(sms.getDate());
                    xmlSerializer.endTag(null, "date");

                    xmlSerializer.startTag(null, "type");
                    xmlSerializer.text(String.valueOf(sms.getType()));
                    xmlSerializer.endTag(null, "type");

                    xmlSerializer.endTag(null, "sms");
                }

                xmlSerializer.endTag(null, "smslist");

                xmlSerializer.endDocument();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 解析XML
     * @param view
     */
    public void read(View view) {
        if (isSDCanUse()) {
            List<SMS> smses = null;
            SMS sms = null;
            XmlPullParser xmlPullParser = Xml.newPullParser();
            try {
                xmlPullParser.setInput(new FileInputStream(new File(Environment.getExternalStorageDirectory(), "/smslist.xml")), "utf-8");
                int eventType = xmlPullParser.getEventType();

                while (eventType != XmlPullParser.END_DOCUMENT) {

                    switch (eventType) {
                        case XmlPullParser.START_TAG:
                            if ("smslist".equals(xmlPullParser.getName())) {
                                smses = new ArrayList<>();
                            } else if ("sms".equals(xmlPullParser.getName())) {
                                sms = new SMS();
                            } else if ("address".equals(xmlPullParser.getName())) {
                                sms.setAddress(xmlPullParser.nextText());
                            } else if ("body".equals(xmlPullParser.getName())) {
                                sms.setBody(xmlPullParser.nextText());
                            } else if ("date".equals(xmlPullParser.getName())) {
                                sms.setDate(xmlPullParser.nextText());
                            } else if ("type".equals(xmlPullParser.getName())) {
                                sms.setType(Integer.parseInt(xmlPullParser.nextText()));
                            }
                            break;
                        case XmlPullParser.END_TAG:
                            if ("sms".equals(xmlPullParser.getName())) {
                                smses.add(sms);
                            }
                            break;
                    }

                    eventType = xmlPullParser.next();
                }

                lvList.setAdapter(new ArrayAdapter<SMS>(this, android.R.layout.simple_list_item_1, smses));

            } catch (XmlPullParserException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 判断SD卡是否可用
     * @return
     */
    private boolean isSDCanUse() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }
}
